package com.feihong.txgw.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.ConvertUtils;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.ICCIDListAdapter;
import com.feihong.txgw.json.kaika.ICCIDList;

import java.util.List;

/**
 * Created by Sabirjan on 2017/7/3.
 * TXGWApp.
 * 飞宏电子
 */

public class ICCIDListPopupWindow {

    private PopupWindow popupWindow;
    private Context mContext;
    private ListView listView;
    private List<ICCIDList.DataBean> items;
    private ICCIDListAdapter adapter;
    private AdapterView.OnItemClickListener listener;

    public ICCIDListPopupWindow(Context context) {
        this.mContext = context;
        popupWindow = new PopupWindow(context);
        listView = (ListView) LayoutInflater.from(context).inflate(R.layout.iccid_popupwindow, null);
        popupWindow.setContentView(listView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        popupWindow.setWidth(ConvertUtils.dp2px(185));
        // 设置SelectPicPopupWindow弹出窗体的高
        popupWindow.setHeight(ConvertUtils.dp2px(200));
        // 设置SelectPicPopupWindow弹出窗体可点击
        popupWindow.setFocusable(false);
        popupWindow.setOutsideTouchable(true);
        // 刷新状态
        popupWindow.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0000000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        popupWindow.setBackgroundDrawable(dw);

    }

    public void setListener(AdapterView.OnItemClickListener listener) {
        if (listener != null) {
            listView.setOnItemClickListener(listener);
        }
    }

    public void show(View view) {

        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
        }

        popupWindow.showAsDropDown(view, 0, 2);
    }

    public void setData(List<ICCIDList.DataBean> items) {
        cleanAll();
        this.items = items;
        adapter = new ICCIDListAdapter(this.mContext, items);
        listView.setAdapter(adapter);
    }

    public void dismiss() {
        popupWindow.dismiss();
    }

    private void cleanAll() {
        if (this.items != null) {
            this.items.clear();
            adapter.notifyDataSetChanged();
            adapter = null;
        }
    }


}
